package com.tanguy.driver.ui.account.activity

import android.app.Activity
import android.content.Intent
import android.text.InputType
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import cn.sinata.xldutils_kotlin.activity.WebViewActivity
import com.tanguy.driver.App
import com.tanguy.driver.Const
import com.tanguy.driver.R
import com.tanguy.driver.base.ImmersionTitleActivity
import com.tanguy.driver.net.http.HttpManager
import com.tanguy.driver.utlis.extend.*
import com.tanguy.user.utlis.extend.click
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult

/**
 * Created by dab on 2017/11/2 0002 09:44
 * 注册第一步
 */
class RegisterFirstActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_register_first
    override fun initView() {
        super.initView()
        title = "司机注册"
    }

    private val mTvCaptcha by lazy { find<TextView>(R.id.tv_get_captcha) }
    private val mTvPlaintext by lazy { find<ImageView>(R.id.tv_plaintext) }
    private val mEtPassword by lazy { find<EditText>(R.id.et_password) }
    private var isPlaintext = false//是明文显示
    override fun initEvent() {
        super.initEvent()
        click(R.id.tv_user_protocol){ startActivity<WebViewActivity>("url" to Const.AppText.Test, "title" to "唐吉司机用户协议") }
        click(R.id.tv_get_captcha) {
            val phone = getEditTextString(R.id.et_phone)
            if (!checkPhoneNumber(phone)) return@click
            HttpManager.sendSmsCode(phone, 4)
                    .requestSucceed(this) {
                        captchaCountDownTimer { finish, msg ->
                            mTvCaptcha.isClickable = finish
                            find<TextView>(R.id.tv_get_captcha).text = msg
                        }
                    }
        }
        click(R.id.tv_plaintext) {
            if (isPlaintext) {
                //密文
                mEtPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                mTvPlaintext.setBackgroundResource(R.mipmap.yanjing_2)
            } else {
                //明文
                mEtPassword.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                mTvPlaintext.setBackgroundResource(R.mipmap.yanjing_1)
            }
            isPlaintext = !isPlaintext
            mEtPassword.setSelection(mEtPassword.text.length)
        }
        click(R.id.btn_next) {
            val phone = getEditTextString(R.id.et_phone)
            if (!checkPhoneNumber(phone)) return@click
            val captcha = getEditTextString(R.id.et_captcha)
            if (!checkCaptcha(captcha)) return@click
            val password = getEditTextString(R.id.et_password)
            if (!checkPassword(password)) return@click
            HttpManager.yzCode(phone, 4, captcha)
                    .requestSucceed(this) {
                        App.app.driverRegister.phone=phone
                        App.app.driverRegister.password=password
                        startActivityForResult<RegisterSecondActivity>(Const.RequestCode.REGISTER)
                    }
            }
        }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.REGISTER) {
                setResult(Activity.RESULT_OK)
                finish()
            }
        }
    }
}